package com.beerq.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.model.UserInfo;
import com.beerq.util.BasicHttp;
import com.beerq.util.BeerQPref;
import com.beerq.util.Constants;
import com.beerq.util.StringUtil;
import com.beerq.view_manager.RegisterAndLoginManager;
import com.beerq.view_manager.ValidateManager;

public class BindPhoneActivity extends BaseActivity {

    Button btnGetValidateCode;

    private EditText mEditTextValidateCode;
    private EditText mEditTextPassword;

    private ValidateManager mValidateManager;

    private String mStrUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType=0;
        super.onCreate(savedInstanceState);
        hideTopBar();
        setMainContentView(R.layout.activity_bind_phone);


        btnGetValidateCode = (Button) findViewById(R.id.btn_getValiCode);
        mEditTextValidateCode=(EditText)findViewById(R.id.edit_validate_code) ;
        mEditTextPassword=(EditText)findViewById(R.id.edit_password) ;

        mValidateManager=new ValidateManager(this);
        mValidateManager.bind();

        mStrUserInfo=getIntent().getStringExtra("USERINFO");

        setEvent();

    }




    private void setEvent(){

        final TextView txtPhoneNo = (TextView) findViewById(R.id.edit_phone_number);


        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNo = txtPhoneNo.getText().toString();
                if (StringUtil.isEmpty(phoneNo)) {
                    showToast("手机号码不能为空");
                    return;
                } else if (phoneNo.length() != 11) {
                    showToast("手机号码格式不正确");
                    return;
                }

                String validateCode = mEditTextValidateCode.getText().toString();
                if (StringUtil.isEmpty(validateCode)) {
                    showToast("验证码不能为空");
                    return;
                }

                String password = mEditTextPassword.getText().toString();
                if (StringUtil.isEmpty(password)) {
                    showToast("密码不能为空");
                    return;
                }

                if(password.length()<7){
                    showToast("密码位数要大于6位");
                    return;
                }

                if(!mValidateManager.checkValidateCode(validateCode)){
                    return;
                }



                if(StringUtil.isEmpty(mStrUserInfo)){
                    return;
                }


                UserInfo userInfo=JSONObject.parseObject(mStrUserInfo,UserInfo.class);


                RegisterAndLoginManager.bindPhoneAndWxNo(mContext, phoneNo, password, userInfo.getWxNo(), userInfo.getUserName(), userInfo.getHeadPic(), new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(String resonseData) {
                        Intent intent = new Intent(mContext, MainActivity.class);

                        JSONObject jsonObject= JSONObject.parseObject(resonseData).getJSONObject("Result");

                        UserInfo userInfo1=new UserInfo();

                        userInfo1.setAppUserId(jsonObject.getIntValue("Id"));
                        userInfo1.setPhoneNo(jsonObject.getString("MobileNo"));
                        userInfo1.setUserName(jsonObject.getString("Name"));
                        userInfo1.setHeadPic(jsonObject.getString("HeadPic"));
                        Constants.userInfo=userInfo1;
                        BeerQPref.getInstance(mContext).setUserInfo(JSONObject.toJSONString(userInfo1));

                        ProductJsManager.setUserInfo(Constants.firstWebView,Constants.userInfo.getAppUserId());

                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onFailure(String resonseData) {

                    }
                });
            }
        });
    }
}
